﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PanGu;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using PatientInformationExtract.MainInterface.Views;

namespace PatientInformationExtract.MainInterface.Models
{
    public class StaticMethod
    {
        public static void DisplayOnRichTextBox(ICollection<WordInfo> words, ref RichTextBoxWithToolBar rtb)
        {
            Paragraph paragraph = new Paragraph();
            rtb.RichTextBox.Document.Blocks.Clear();
            rtb.RichTextBox.Document.Blocks.Add(paragraph);
            List<Run> displayRuns = new List<Run>();
            List<WordInfo> wordsReplaceWithSynonym = new List<WordInfo>();            
            foreach (WordInfo word in words)
            {
                if (word.WordType.Equals(WordType.Synonym))
                {
                    wordsReplaceWithSynonym.RemoveAt(wordsReplaceWithSynonym.Count - 1);
                    displayRuns.Last().Text += @"(*" + word.Word + ")";
                }
                else
                {
                    displayRuns.Add(new Run(word.Word));
                }
                wordsReplaceWithSynonym.Add(word);
            }
            for (int i = 0; i < wordsReplaceWithSynonym.Count; i++)
            {
                WordInfo word = wordsReplaceWithSynonym[i];
                switch (word.Pos)
                {
                    case POS.POS_U_O:
                        {
                            bool match = false;
                            int position = 0;
                            if (rtb.IsObservation)
                            {
                                if (i != 0 && i != words.Count)
                                {
                                    if (wordsReplaceWithSynonym[i-1].Pos.Equals(POS.POS_U_D))
                                    {
                                        match = !match;
                                        position = -1;
                                    }
                                    if (wordsReplaceWithSynonym[i+1].Pos.Equals(POS.POS_U_D))
                                    {
                                        match = !match;
                                        position = 1;
                                    }
                                }
                                if (match)
                                {
                                    displayRuns[i + position].Foreground = Brushes.Red;
                                    displayRuns[i + position].TextDecorations = TextDecorations.Underline;
                                    displayRuns[i + position].FontStyle = FontStyles.Italic;
                                    displayRuns[i].Foreground = Brushes.Red;
                                    displayRuns[i].TextDecorations = TextDecorations.Underline;
                                    displayRuns[i].FontStyle = FontStyles.Italic;
                                }
                                else
                                {
                                    displayRuns[i].Foreground = Brushes.Red;
                                }
                            }
                            break;
                        }
                    case POS.POS_U_M:
                        {
                            if (rtb.IsMedicalIntervention)
                            {
                                displayRuns[i].Foreground = Brushes.Blue;
                            }
                            break;
                        }
                    case POS.POS_U_T:
                        {
                            if (rtb.IsTime)
                            {
                                displayRuns[i].Foreground = Brushes.Green;
                            }
                            break;
                        }
                }
            }
            foreach (Run run in displayRuns)
            {
                paragraph.Inlines.Add(run);
            }
        }
    }
}
